package com.huitone.gddw.controller.worktable.paramconfigmanage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.SvrRouteRule;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISvrRouteRuleDao;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.ISvrRouteRuleService;
import com.huitone.gddw.service.dto.GateSvrLevelDTO;
import com.huitone.gddw.service.dto.SvrGateLevelDTO;
import com.huitone.gddw.service.dto.VirtualGateLinkDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 路由通道控制器
 * @author Weiliyu
 *
 */
@Controller
@RequestMapping(value="routeGate")
public class RouteGateController extends CommonController {
	
	private final static Logger LOGGER = Logger.getLogger(RouteGateController.class);

	@Autowired
	private ISvrRouteRuleService svrRouteRuleService;
	@Autowired
	private ISvrRouteRuleDao svrRouteRuleDao;
	/**
	 * 根据短信类型查询路由通道
	 * @param page
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="queryByServiceCode")
	public QueryPage<SvrGateLevelDTO> query(QueryPage<SvrRouteRule> page, HttpServletRequest request){
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]根据短信类型查询路由通道";
		Map<String, Object> params = new HashMap<String, Object>();
		String serviceCode = request.getParameter("serviceCode");
		String gateLinkId = request.getParameter("gateLinkId");
		params.put("serviceCode", serviceCode);
		params.put("gateLinkId", gateLinkId);
		page.setParams(params);
		QueryPage<SvrGateLevelDTO> result = null;
		try {
			result = svrRouteRuleService.selectPageByServiceCode(page);
		} catch (Exception e) {
			LOGGER.error("根据短信类型查询路由通道信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 保存
	 * @param entity SvrRouteRule
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(SvrGateLevelDTO entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加路由通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			if (null==entity.getServiceCode() || entity.getServiceCode()=="") {
				return new Message(false, "短信类型不为空！");
			}
			HashMap<String,Object> map = new HashMap<>();
			map.put("serviceCode", entity.getServiceCode());
			int count = svrRouteRuleDao.selectCountByServiceCode(map);
			if (count > 0) {
				return new Message(false, "该短信类型已存在！");
			}
			Message message = svrRouteRuleService.saveRouteGate(entity,"1");	//1:保存
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return message;
		} catch (Exception e) {
			LOGGER.error("保存路由通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(isSuccess, msg);
		}
	}
	
	/**
	 * 获取修改信息
	 * @param serviceCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public SvrGateLevelDTO toUpdate(String serviceCode){
		try {
			SvrGateLevelDTO entity = svrRouteRuleService.getRouteGate(serviceCode);
			return entity;
			
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new SvrGateLevelDTO();
		}
	}
	
	/**
	 * 修改
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(SvrGateLevelDTO entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改路由通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			if (null==entity.getServiceCode() || entity.getServiceCode()=="") {
				return new Message(false, "短信类型不为空！");
			}
			if(entity.getServiceCode().equals(entity.getOldServiceCode())){
				Message message = svrRouteRuleService.saveRouteGate(entity,"2");	//2:修改
				super.saveSmLog(super.logType, super.logContent, super.logResult);
				return message;	
			}else{
				HashMap<String,Object> map = new HashMap<>();
				map.put("serviceCode", entity.getServiceCode());
				int count = svrRouteRuleDao.selectCountByServiceCode(map);
				if (count > 0) {
					return new Message(false, "该短信类型已存在！");
				}else {
					Message message = svrRouteRuleService.saveRouteGate(entity,"2");	//2:修改
					super.saveSmLog(super.logType, super.logContent, super.logResult);
					return message;
				}	
			}
			
		} catch (Exception e) {
			LOGGER.error("修改路由通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(isSuccess, msg);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String serviceCode) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除路由通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			svrRouteRuleService.deleteByServiceCode(serviceCode);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除路由通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 根据通道查询
	 * @param page
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="queryByGate")
	public QueryPage<GateSvrLevelDTO> queryByGate(QueryPage<SvrRouteRule> page, HttpServletRequest request){
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]根据通道类别查询路由通道";
		Map<String, Object> params = new HashMap<String, Object>();
		String serviceCode = request.getParameter("serviceCode");
		String gateLinkId = request.getParameter("gateLinkId");
		params.put("serviceCode", serviceCode);
		params.put("gateLinkId", gateLinkId);
		page.setParams(params);
		QueryPage<GateSvrLevelDTO> result = null;
		try {
			result = svrRouteRuleService.selectPageByGateLinkId(page);
		} catch (Exception e) {
			LOGGER.error("根据通道类别查询路由通道信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="saveByGate")
	public Message saveByGate(GateSvrLevelDTO entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加路由通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			if (null==entity.getGateLinkId() || entity.getGateLinkId()=="") {
				return new Message(false, "通道名称不为空！");
			}
			HashMap<String,Object> map = new HashMap<>();
			map.put("gateLinkId", entity.getGateLinkId());
			int count = svrRouteRuleDao.selectCountByGateLinkId(map);
			if (count > 0) {
				return new Message(false, "该通道已存在！");
			}
			
			Message message = svrRouteRuleService.saveRouteGate2(entity,"1");	//1:保存
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return message;
		} catch (Exception e) {
			LOGGER.error("保存路由通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(isSuccess, msg);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="toUpdateByGate")
	public GateSvrLevelDTO toUpdateByGate(String gateLinkId){
		try {
			GateSvrLevelDTO entity = svrRouteRuleService.getRouteGateByGateLinkId(gateLinkId);
			return entity;
			
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new GateSvrLevelDTO();
		}
	}
	
	@ResponseBody
	@RequestMapping(value="updateByGate")
	public Message updateByGate(GateSvrLevelDTO entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改路由通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			if (null==entity.getGateLinkId() || entity.getGateLinkId()=="") {
				return new Message(false, "通道名称不为空！");
			}
			
			
			if(entity.getGateLinkId().equals(entity.getOldGateLinkId())){
				Message message = svrRouteRuleService.saveRouteGate2(entity, "2");	//2:修改;
				super.saveSmLog(super.logType, super.logContent, super.logResult);
				return message;
			}else{
				HashMap<String,Object> map = new HashMap<>();
				map.put("gateLinkId", entity.getGateLinkId());
				int count = svrRouteRuleDao.selectCountByGateLinkId(map);
				if (count > 0) {
						return new Message(false, "该通道已存在！");
					}else {
					Message message = svrRouteRuleService.saveRouteGate2(entity, "2");	//2:修改;
					super.saveSmLog(super.logType, super.logContent, super.logResult);
					return message;
					}	
			}

		} catch (Exception e) {
			LOGGER.error("修改路由通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(isSuccess, msg);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="deleteByGate")
	public Message deleteByGate(String gateLinkId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除路由通道信息";
		boolean isSuccess = true;
		String msg;
		try {
			svrRouteRuleService.deleteByGateLinkId(gateLinkId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除路由通道信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
}
